package mianyang.zhangsan.com.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.pojo.Score;
import mianyang.zhangsan.com.pojo.ScoreCustom;
import mianyang.zhangsan.com.pojo.ScoreStats;
import mianyang.zhangsan.com.service.CourseService;
import mianyang.zhangsan.com.service.ScoreService;
import mianyang.zhangsan.com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/score")
public class ScoreController {
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;


    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(value = "page", defaultValue = "1")int page,
                                    @RequestParam(value = "rows", defaultValue = "10")int rows,
                                    @RequestParam(value = "studentId", defaultValue = "0")int studentId,
                                    @RequestParam(value = "courseId", defaultValue = "0")int courseId){
        Page<Score> scorePage = scoreService.queryScoreByPage(page, rows, studentId, courseId);
        Map<String, Object> map = new HashMap<>();
        ScoreCustom scoreCustom = new ScoreCustom();
        List<ScoreCustom> scoreCustoms = new ArrayList<>();
        for (Score score:scorePage.getRecords()){
            //根据studentId查找studentName
            String studentName = studentService.queryUserNameById(score.getStudentId());
            //根据courseId查找courseName
            String courseName = courseService.queryNameById(score.getCourseId());
            scoreCustom = scoreService.scorePackage(score, studentName,courseName);
            scoreCustoms.add(scoreCustom);
        }
        map.put("total", scorePage.getPages());
        map.put("rows", scoreCustoms);
        return map;
    }

    /**
     * 添加成绩
     * @param score
     * @return
     */
    @RequestMapping(value = "/add", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String add(Score score){
        System.out.println(score);
        if (score != null){
            scoreService.scoreAdd(score);
            return "{\"success\": true,\"message\":\"添加成功\"}";
        }
        return "{\"success\": false}";
    }

    /**
     * 删除成绩
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(int id){
        if (id != 0){
            scoreService.delete(id);
            return "{\"success\": true, \"message\": \"删除成功\"}";
        }
        return "{\"success\": false}";
    }

    /**
     * 成绩统计
     * @param courseId
     * @param searchType
     * @return
     */
    @RequestMapping("/compute")
    @ResponseBody
    public Object compute(int courseId, String searchType){
        String courseName = courseService.queryNameById(courseId);
        if ("avg".equals(searchType)){
            ScoreStats scoreStats = scoreService.queryScoreStatsByCourseId(courseId);
            List<Double> scoreList = new ArrayList<>();
            scoreList.add(scoreStats.getMaxScore());
            scoreList.add(scoreStats.getMinScore());
            scoreList.add(scoreStats.getAvgScore());

            List<String> descList = new ArrayList<>();
            descList.add("最高分");
            descList.add("最低分");
            descList.add("平均分");

            Map<String, Object> map = new HashMap<>();
            map.put("courseName", courseName);
            map.put("scoreList", scoreList);
            map.put("avgList", descList);
            map.put("type", "success");
            return map;
        }
        List<Score> scoreList = scoreService.queryScoreByCourseId(courseId);
        List<Integer> numberList = new ArrayList<Integer>();
        numberList.add(0);
        numberList.add(0);
        numberList.add(0);
        numberList.add(0);
        numberList.add(0);

        List<String> rangeStringList = new ArrayList<String>();
        rangeStringList.add("60分以下");
        rangeStringList.add("60~70分");
        rangeStringList.add("70~80分");
        rangeStringList.add("80~90分");
        rangeStringList.add("90~100分");
        for(Score score : scoreList){
            double scoreValue = score.getScore();
            if(scoreValue < 60){
                numberList.set(0, numberList.get(0)+1);
                continue;
            }
            if(scoreValue <= 70 && scoreValue >= 60){
                numberList.set(1, numberList.get(1)+1);
                continue;
            }
            if(scoreValue <= 80 && scoreValue > 70){
                numberList.set(2, numberList.get(2)+1);
                continue;
            }
            if(scoreValue <= 90 && scoreValue > 80){
                numberList.set(3, numberList.get(3)+1);
                continue;
            }
            if(scoreValue <= 100 && scoreValue > 90){
                numberList.set(4, numberList.get(4)+1);
                continue;
            }
        }
        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("courseName", courseName);
        retMap.put("numberList", numberList);
        retMap.put("rangeList", rangeStringList);
        retMap.put("type", "success");
        return retMap;
    }
}
